<template>
  <div class="checkuser">
    <hunk-head name="马上安家"></hunk-head>
    <div class="apprv">
      <div class="wxpopup">
        <img style="width:360rpx;height: 360rpx;margin: 40rpx auto;border-radius: 50%;" v-if="baseUrl" :src="baseUrl+'imgs/imgs/header.jpg'">
        <button  v-if="!isUserAuth" class="btn active flex" >
          <img style="width: 60rpx;height: 60rpx;margin: 10rpx;" v-if="baseUrl" :src="baseUrl+'imgs/imgs/phone.png'">
          微信授权手机号码
        </button>
        <button v-if="isUserAuth" open-type="getPhoneNumber" class="btn fcenter flex" @getphonenumber="getPhoneNumber" >
          <img style="width: 60rpx;height: 60rpx;margin: 10rpx;" v-if="baseUrl" :src="baseUrl+'imgs/imgs/phone.png'">
          微信授权手机号码
        </button>
        <view style="display: flex;justify-content: center;align-items: center;width: 80%;margin: 40rpx auto;">
          <radio-group>
            <radio :value="value" :checked="value" @click.stop="radioChange" />
          </radio-group>
          <view style="font-size: 24rpx;text-align: left;">
            登录用于记录用户在该平台的使用数据方便用户更好的体验小程序。已阅读并同意 <text style="color: rgba(59, 59, 165, 0.767);" @click.stop="jumptoxy">《用户协议和隐私政策》</text>，同时同意获取您的昵称和头像用来完善基本信息
          </view>
        </view>
      </div>
    </div>
  </div>
</template>

<script>
import Head from '@componentsMine/head'
import store from '@store/index'
import {Reguser,getTelphone,GetUserInfo_openid,getuseropenid,ChangeUserType} from '@/apis/user'
import { setToken } from '@utils/auth'
export default {
  components: {
    'hunk-head': Head
  },
  data() {
    return {
      phoneShow:false,
      isUserAuth:false,
      value:false,
      isCode: false,
      type: 0
    }
  },
  // onLoad(e){
  //   this.isShouquan()
  // },
  computed: {
    baseUrl() {
      return process.env.VUE_APP_BASE_IMG_URL
    }
  },
  methods: {
    jumptoxy(){
      uni.navigateTo({ url: '/pages/login/privacy' })
    },
    radioChange(){
      console.log(111,this.value)
      this.value = !this.value
      if(this.value){
        this.getUserProfile()
      }
      this.$forceUpdate()
    },
    getPhoneNumber(e){
      let that = this
      if(!this.isUserAuth){
        uni.showToast({
          title: '请先确认用户授权！',
          icon: 'none'
        })
        return
      }
      console.log('获取手机号111',e)
      wx.setStorageSync('phoneBntInfo',JSON.stringify(e.detail))
      getTelphone({encryptedData:e.detail.encryptedData,iv:e.detail.iv,session_key:wx.getStorageSync('session_key')}).then(e=>{
        console.log('手机号登陆成功',e.data)
          wx.setStorageSync('phone',e.data.telphone)
          const userinfo = JSON.parse(wx.getStorageSync('userInfo'))
          const openid = wx.getStorageSync('openid')
          const telphone = wx.getStorageSync('phone')
          const {avatarUrl,gender,nickName} = JSON.parse(userinfo.rawData)
          Reguser({
            "avatar": avatarUrl,
            "openid": openid,
            "sex": gender,
            "telphone": telphone,
            "unickname": nickName
          }).then(e=>{
            debugger
            setToken(e.data.token)
            console.log('用户注册成功',e.data)
            ChangeUserType({userid:e.data.userid,type: that.type}).then(e=>{
              if(e.code == 200){
                  wx.setStorageSync('sysUserInfo',JSON.stringify(e.data))
                  console.log('用户选择成功',e.data)
                  setToken(e.data.token)
                  store.commit('baseStore/SET_USERINFO', e.data)
                  // uni.redirectTo({ url: '/pages/index/index' })
                  uni.navigateBack({ delta: 1 })
                }
            }).catch(e=>{
              console.log(e)
            })
          
          })
      })
    },
    getUserProfile(){
      const that = this
      wx.showModal({
        title: '授权登陆',
        content: '是否继续授权您的昵称和头像来完善基本信息',
        success(res) {
          if (res.confirm) {
            wx.getUserProfile({
              desc: '用于完善信息资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
              success: (res) => {
                console.log('99999信息资料',res)
                wx.setStorageSync('userInfo',JSON.stringify(res))
                that.isUserAuth = true
              },
              fail:()=>{
                that.value = false
              }
            })
          }else{
            that.value = false
            // wx.switchTab({
            //   url: '/pages/index/index',
            // })
          }
        }
      })
    },
    isShouquan(){
      let that = this
      wx.login({
        success:(res)=> {
          console.log('login',res)
          var code = res.code
          getuseropenid({code}).then(e=>{
            console.log('getuseropenid',typeof(e.data.openid),e.data.openid)
            // if(e.data.openid != 'null'){
              wx.setStorageSync('openid', e.data.openid)
              wx.setStorageSync('session_key', e.data.session_key)
              GetUserInfo_openid({openid:e.data.openid}).then(e=>{
                console.log('执行GetUserInfo_openid',e)
                that.isCode = true
                if(e.code == 200){
                  wx.setStorageSync('cardid', e.data.cardid)
                  store.commit('baseStore/SET_USERINFO', e.data)
                  setToken(e.data.token)
                  // that.$jump({path: '/pages/index/index'})
                  uni.redirectTo({ url: '/pages/index/index' })
                }
              }).catch(e=>{
                
              })
            // }else{
            //   that.isCode = false
            //   that.$refs.wxpopup.open()
            // }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.checkuser {
  @include num();
  display: flex;
  flex-direction: column;
  .apprv{
    flex: 1;
    display: flex;
    justify-content: center;
    // align-items: center;
    margin-top: 40px;
  }
  .wxpopup{
    width: 335px;
    background: #FFFFFF;
    display: flex;
    flex-direction: column;
    align-items: center;
    border-radius: 10px;
    .or{
      background:#19be6b;
    }
    .green{
      background:#2d8cf0;
    }
    .red{
      background:#1c2438;
    }
    .btn{
      width: 500rpx;
      display: flex;
      height: 80rpx;
      font-size: 26rpx;
      line-height: 80rpx;
      margin-top: 10rpx;
      background-color: #19be6b;
      color: #fff;
    }
    .btn.active{
        background: #71dcae;
    }
    .flex{
      display: flex;
      justify-content: center;
      align-content: center;
      flex-direction: row;
    }
    .btn::after{
      border: 0;
    }
  }
}
</style>
